//
//  YSUserModel.m
//  Yaoyle
//
//  Created by ZhaoXudong on 14-9-19.
//  Copyright (c) 2014年 ZhaoXudong. All rights reserved.
//

#import "YSUserModel.h"
#import "YSUtility.h"
#import "YSDefine.h"

#define gUserInfo    @"gUserInfo"

@implementation YSUserModel

- (id)init {
    self = [super init];
    if (self) {
        NSDictionary *userInfo = DefaultValueForKey(gUserInfo);
        self.userId = [userInfo[UserInfoId] intValue];
        self.mobile = userInfo[UserInfoMobile];
        self.name = userInfo[UserInfoName];
        self.photoUrl = userInfo[UserInfoPhotoUrl];
        self.sex = [userInfo[UserInfoSex] boolValue];
        self.scoreAmount = userInfo[UserInfoScore];
    }
    return self;
}

- (id)initFromUserInfo:(NSDictionary *)userInfo {
    self = [super init];
    if (self) {
        if ([YSUtility isBlankObject:userInfo[@"name"]]) {
            if ([YSUtility isBlankObject:userInfo[@"newGamerName"]]) {
                self.name = @"";
            } else {
                self.name = userInfo[@"newGamerName"];
            }
        } else {
            self.name = userInfo[@"name"];
        }
        
        if ([YSUtility isBlankObject:userInfo[@"mobile"]]) {
            self.mobile = @"";
        } else {
            self.mobile = userInfo[@"mobile"];
        }
        
        if ([YSUtility isBlankObject:userInfo[@"sex"]]) {
            self.sex = YES;
        } else {
            self.sex = [userInfo[@"sex"] boolValue];
        }
        
        if ([YSUtility isBlankObject:userInfo[@"scoreAmount"]]) {
            self.scoreAmount = @"";
        } else {
            if (![userInfo[@"scoreAmount"] isKindOfClass:[NSString class]]) {
                NSNumber *numb = [NSNumber numberWithLongLong:[userInfo[@"scoreAmount"] integerValue]];
                self.scoreAmount = [numb stringValue];
            } else {
                self.scoreAmount = userInfo[@"scoreAmount"];
            }
        }
        
        if ([YSUtility isBlankObject:userInfo[@"photoUrl"]]) {
            self.photoUrl = @"";
        } else {
            self.photoUrl = userInfo[@"photoUrl"];
        }
        
        if ([YSUtility isBlankObject:userInfo[@"id"]]) {
            self.userId = 0;
        } else {
            if (![userInfo[@"id"] isKindOfClass:[NSString class]]) {
                NSNumber *numb = [NSNumber numberWithLongLong:[userInfo[@"id"] integerValue]];
                self.userId = [numb integerValue];
            } else {
                self.userId = [userInfo[@"id"] integerValue];
            }
        }
        
        self.status = 0;
        if (![YSUtility isBlankObject:userInfo[@"status"]]) {
            self.status = [userInfo[@"status"] integerValue];
        }
        
    }
    return self;
}

- (void)saveUserInfo {
    NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
    DictSetValueForKey(userInfo, self.mobile, UserInfoMobile);
    DictSetValueForKey(userInfo, self.name, UserInfoName);
    DictSetValueForKey(userInfo, self.scoreAmount, UserInfoScore);
    NSString *value = [NSString stringWithFormat:@"%d",self.sex];
    DictSetValueForKey(userInfo, value, UserInfoSex);
    value = [NSString stringWithFormat:@"%d",self.userId];
    DictSetValueForKey(userInfo, value, UserInfoId);
    DictSetValueForKey(userInfo, self.photoUrl, UserInfoPhotoUrl);
    
    DefaultSetValueForKey(userInfo, gUserInfo);
}

+ (BOOL)isLogin {
    NSDictionary *userInfo = DefaultValueForKey(gUserInfo);
    if ([YSUtility isBlankObject:userInfo]) {
        return NO;
    }
    return YES;
}

- (BOOL)logout {
    // remove user image
    RemoveDefaultObjectForKey(gUserInfo);
    return YES;
}

- (BOOL)isHaveRole {
    if ([YSUtility isBlankObject:self.name]) {
        return NO;
    }
    return YES;
}
@end
